var chargeSchedule = {
    id: "chargeScheduleTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

chargeSchedule.initColumn = function () {// 初始化表格的列
    var columns = [
        {field: 'selectItem', radio: true},
        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle'},
        {title: 'id2', field: 'id2', visible: false, align: 'center', valign: 'middle'},
        {title: '医生', field: 'doctor', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '项目名称', field: 'payServiceName', align: 'center', valign: 'middle', sortable: true, width: 400},
        {title: '数量', field: 'payServiceNum', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '折后价', field: 'foldingPay', align: 'center', valign: 'middle', sortable: true, width: 100},
        {title: '实收金额', field: 'serviceActualPay', align: 'center', valign: 'middle', sortable: true, width: 100}
    ]
    return columns;
};

chargeSchedule.search = function () {
    chargeSchedule.table.refresh({query: chargeSchedule.formParams()});
    chargeSchedule.totalMoney();
};

$("#doctorSearchAll", parent.document).click(function () {//主页搜索
    chargeSchedule.search();
});

chargeSchedule.resetSearch = function () {
    $("#startTime").val("");
    $("#endTime").val("");
    $("#payServiceName").val("");
    chargeSchedule.inquire()
};

chargeSchedule.inquire = function () {
    chargeSchedule.search();
    chargeSchedule.totalMoney();
};

chargeSchedule.close = function () {parent.layer.close(parent.layer.getFrameIndex(window.name));};

chargeSchedule.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['payServiceName'] = $("#payServiceName").val();
    return queryData;
};

var parseParam = function (param) {//把对象拼接成URL的键值对参数param1=value1&param2=value2
    var paramStr = "";
    $.each(param, function (i) {
        paramStr += '&' + getSingleData(param, i);
    });
    return paramStr.substr(1);
};
var getSingleData = function (param, key) {return key + "=" + param[key];};

chargeSchedule.InfoExport = function () {//导出数据
    var queryData = {};
    queryData['reach'] = $("#doctorInputPublic", parent.document).val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['payServiceName'] = $("#payServiceName").val();
    var param = parseParam(queryData);
    location.href = Feng.ctxPath + '/doctorLooked/chargeScheduleExport?' + param;
};

var myDate = new Date();
var month;
var day;
if ((myDate.getMonth() + 1) < 10) {
    month = "0" + (myDate.getMonth() + 1);
} else {
    month = "" + (myDate.getMonth() + 1);
};
if (myDate.getDate() < 10) {
    day = "0" + myDate.getDate();
} else {
    day = "" + myDate.getDate();
};

chargeSchedule.totalMoney = function () {
    var reach = $("#doctorInputPublic", parent.document).val();
    var startTime = document.getElementById("startTime").value;
    var endTime = document.getElementById("endTime").value;
    var payServiceName = document.getElementById("payServiceName").value;
    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/doctorLooked/totalMoney",
        data: {
            reach: reach,
            startTime: startTime,
            endTime: endTime,
            payServiceName: payServiceName
        },
        dataType: "json",
        success: function (data) {
            $("#totalmoney").text(data);//总金额
        }
    });
};

$(function () {
    document.getElementById("startTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "00:00:00";
    document.getElementById("endTime").value = myDate.getFullYear() + "-" + month + "-" + day + " " + "23:59:59";
    var defaultColunms = chargeSchedule.initColumn();
    var table = new BSTable(chargeSchedule.id, "/doctorLooked/getChargeSchedule", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(chargeSchedule.formParams());
    chargeSchedule.table = table.init();
    chargeSchedule.search();
    chargeSchedule.totalMoney();
    var startTimeTwo = $("#startTime").val();
    var endTimeTwo = $("#endTime").val();
    setInterval(//采用定时器监听 时间内容是否变化
        function () {//定时执行方法内容
            if ($("#startTime").val() != startTimeTwo || $("#endTime").val() != endTimeTwo) {//如果时间发生变化，就触发查询事件
                chargeSchedule.search();
                chargeSchedule.totalMoney();
                startTimeTwo = $("#startTime").val();//如果值发生变化之后，把最新的值赋值到老的变量里面
                endTimeTwo = $("#endTime").val();
            }
        }, 500);
});